package com.zbf.order.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
import com.zbf.order.entity.MyUser;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/24  8:36
 * 描述:
 */
@RestController
public class TestOKHttpInterfaceController {

    @RequestMapping("testGet")
    public Object testGetHttp(HttpServletRequest request,MyUser myUser){

        System.out.println("=========请求参数====="+myUser);

        System.out.println("=========请求头参数==token==="+request.getHeader("token"));

        Map<String,Object> mmm=new HashMap<>();
        mmm.put("returnData","响应结果");

       /*if(myUser!=null){
            throw new RuntimeException("=========出错了========");
        }*/

        return mmm;

    }


    /**
     * 作者: LCG
     * 日期: 2021/3/24  9:22
     * 描述: 通过RequestBody传参
     * @Param [request, myUser]
     * @Return java.lang.Object
     */
    @RequestMapping("testPost1")
    public Object testPost1Http(HttpServletRequest request,@RequestBody MyUser myUser){

        System.out.println("=========请求参数====="+myUser);

        System.out.println("=========请求头参数==token==="+request.getHeader("token"));

        Map<String,Object> mmm=new HashMap<>();
        mmm.put("returnData","响应结果");

       /*if(myUser!=null){
            throw new RuntimeException("=========出错了========");
        }*/

        return mmm;

    }

    @RequestMapping("testPost2")
    public Object testPost2(HttpServletRequest request,MyUser myUser){

        System.out.println("=====参数===="+myUser);
        System.out.println("=====头参数===="+request.getHeader("token"));

        Map<String,Object> mmm=new HashMap<>();
        mmm.put("returnData","响应结果testPost2");

        return mmm;
    }


    /**
     * 作者: LCG
     * 日期: 2021/3/24  9:41
     * 描述: 使用Base64 编码传递文件
     * @Param [request, myUser]
     * @Return java.lang.Object
     */
    @RequestMapping("testPost3")
    public Object testPost3(MyUser myUser) throws IOException {

        System.out.println("=====参数===="+myUser);


        BASE64Decoder base64Decoder=new BASE64Decoder();

        byte[] bytes = base64Decoder.decodeBuffer(myUser.getImage());

        FileOutputStream fileOutputStream=new FileOutputStream(new File("E:/IMAGE/"+myUser.getFilename()));
        fileOutputStream.write(bytes);

        fileOutputStream.close();


        Map<String,Object> mmm=new HashMap<>();
        mmm.put("returnData","响应结果testPost2");

        return mmm;
    }


    @RequestMapping("testPost4")
    public Object testPost4(MultipartFile[] files,MyUser myUser) throws IOException {

        for (MultipartFile file : files) {
            file.transferTo(new File("E:/IMAGE/"+file.getOriginalFilename()));
        }

        System.out.println("======form-data====="+myUser);

        Map<String,Object> mmm=new HashMap<>();
        mmm.put("returnData","响应结果OK  no problem");
        return mmm;

    }

}
